package com.tbq.tbqj_ggl.activity

import android.app.Activity
import android.os.Bundle
import android.view.KeyEvent
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.tbq.sdk.L
import com.tbq.sdk.TBQ
import com.tbq.sdk.TBQ_V
import com.tbq.tbqj_ggl.app.BaseActivity
import com.tbq.tbqj_ggl.model.Prizes
import com.tbq.tbqj_ggl.utils.EmptyUtil
import com.tbq.tbqj_ggl.utils.RandomUtil
import com.tbq.tbqj_ggl.windows.ScratchGameFailWindows
import com.tbq.tbqj_ggl.windows.ScratchGameWinWindows
import com.tbq.tbqj_ggl.windows.TowerGameExitWindows

/**
 *
 * Created by hais1992 on 2017/7/26.
 */
abstract class ScrachGameBaseActivity(var layoutId: Int) : BaseActivity() {
    var isPlayGameing = false   //是否正在游戏中
    val scratchGameFailWindows by lazy { ScratchGameFailWindows(this) }
    val scratchGameWinWindows by lazy { ScratchGameWinWindows(this) }

    var data_goods_list: MutableList<Prizes>? = null
    var challengePrize: Prizes? = null    //要挑战的奖品
    open var SERVER_URL = "http://"    //要挑战的奖品

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(layoutId)

        SERVER_URL = TBQ.get(TBQ_V.KEY_SERVER_URL)

        when (TBQ.get(TBQ_V.KEY_PLAY_GAME)) {
            "100" -> {   //宝塔
                data_goods_list = arrayListOf(
                        Prizes(100, "/images/bximg/baoxiang1.png"),
                        Prizes(200, "/images/bximg/baoxiang2.png"),
                        Prizes(300, "/images/bximg/baoxiang3.png"),
                        Prizes(400, "/images/bximg/baoxiang4.png"),
                        Prizes(500, "/images/bximg/baoxiang5.png"),
                        Prizes(600, "/images/bximg/baoxiang6.png"),
                        Prizes(700, "/images/bximg/baoxiang7.png"),
                        Prizes(800, "/images/bximg/baoxiang8.png"),
                        Prizes(900, "/images/bximg/baoxiang9.png"),
                        Prizes(1000, "/images/bximg/baoxiang10.png"))
                challengePrize = (data_goods_list as ArrayList<Prizes>)[RandomUtil.getRandom(0, 9)]
            }
            else -> {
                challengePrize = Gson().fromJson(TBQ.get(TBQ_V.KEY_PRIZE_JSON), Prizes::class.java)
                data_goods_list = Gson().fromJson(TBQ.get(TBQ_V.KEY_ALL_PRIZES_LIST), object : TypeToken<ArrayList<Prizes>>() {}.type)
            }
        }
    }

    override fun onDrawFinish() {
        super.onDrawFinish()
        if (EmptyUtil.emptyOfList(data_goods_list)) {
            L.showShort("暂未设置奖品,请联系商家设置奖品.")
            finish()
        } else {
            initDataAndView()
            initDataToStartGame(challengePrize!!, TBQ.get(TBQ_V.KEY_IS_WIN) == "1")
        }
    }

    abstract fun initDataAndView()

    open fun initDataToStartGame(challengePrize: Prizes, isWin: Boolean) {
        isPlayGameing = true
    }

    open fun showGameResultWindows(isWin: Boolean) {
        isPlayGameing = false
        if (isWin) {
            scratchGameWinWindows.showCenter()
        } else {
            scratchGameFailWindows.showCenter()
        }
    }

    override fun onKeyDown(keyCode: Int, event: KeyEvent): Boolean {
        if (keyCode == KeyEvent.KEYCODE_BACK) { //监控/拦截/屏蔽返回键
            TowerGameExitWindows(this).showCenter()
            return false
        } else {
            return super.onKeyDown(keyCode, event)
        }
    }

}